<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>加密解密测试</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .header h1 {
            margin: 0;
            font-size: 2.5em;
            font-weight: 300;
        }
        .content {
            padding: 30px;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            background: #f9f9f9;
        }
        .section h3 {
            margin-top: 0;
            color: #333;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
        }
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            font-size: 16px;
            border-radius: 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
        }
        .btn:active {
            transform: translateY(0);
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            max-height: 300px;
            overflow-y: auto;
        }
        .success {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔐 加密解密测试</h1>
            <p>SpringBoot + SM2/SM4 加密解密演示</p>
        </div>
        
        <div class="content">
            <div class="section">
                <h3>📤 发送加密请求</h3>
                <p>点击下面的按钮发送一个加密的POST请求到服务器，服务器会解密请求体并在控制台输出明文。</p>
                <button class="btn" onclick="sendEncryptedRequest()">🚀 发送加密请求</button>
                <div class="loading" id="loading">
                    <div class="spinner"></div>
                    <p>正在处理请求...</p>
                </div>
                <div id="result" class="result" style="display: none;"></div>
            </div>
            
            <div class="section">
                <h3>📋 请求详情</h3>
                <div class="info">
                    <strong>请求URL:</strong> POST /api/test<br>
                    <strong>请求头:</strong> X-Encrypt-Flag: [加密的SM4密钥]<br>
                    <strong>请求体:</strong> [加密的JSON数据]<br>
                    <strong>加密算法:</strong> SM2(密钥交换) + SM4(数据加密)
                </div>
            </div>
            
            <div class="section">
                <h3>🔍 响应详情</h3>
                <div class="info">
                    <strong>响应头:</strong> X-Encrypt-Flag: [加密的SM4密钥]<br>
                    <strong>响应体:</strong> [加密的JSON数据]<br>
                    <strong>解密后:</strong> 服务器返回的明文数据
                </div>
            </div>
        </div>
    </div>

    <!-- 引入真实的SM2/SM4加密库 -->
    <script src="https://cdn.jsdelivr.net/npm/sm-crypto@0.3.12/dist/sm-crypto.min.js"></script>
    <script src="/js/crypto.js"></script>
    <script>
        async function sendEncryptedRequest() {
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            
            loading.style.display = 'block';
            result.style.display = 'none';
            
            try {
                // 准备请求数据
                const requestData = {
                    message: "这是客户端发送的明文数据",
                    timestamp: Date.now(),
                    user: "test_user",
                    action: "encrypt_test",
                    random: Math.random().toString(36).substring(7)
                };
                
                console.log("原始请求数据:", requestData);
                
                // 使用加密工具库加密数据
                const encryptedResult = CryptoUtils.encryptRequest(requestData);
                
                console.log("加密结果:", encryptedResult);
                
                // 发送请求
                const response = await fetch('/api/test', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-Encrypt-Flag': encryptedResult.encryptedKey
                    },
                    body: encryptedResult.encryptedData
                });
                
                const responseText = await response.text();
                const responseHeaders = {};
                response.headers.forEach((value, key) => {
                    responseHeaders[key] = value;
                });
                
                console.log("服务器响应头:", responseHeaders);
                console.log("服务器响应体:", responseText);
                
                // 尝试解密响应（如果响应头包含加密标识）
                let decryptedResponse = null;
                if (responseHeaders['x-encrypt-flag']) {
                    try {
                        decryptedResponse = CryptoUtils.decryptResponse(responseText, responseHeaders['x-encrypt-flag']);
                        console.log("解密后的响应:", decryptedResponse);
                    } catch (error) {
                        console.error("响应解密失败:", error);
                    }
                }
                
                // 显示结果
                result.className = 'result success';
                result.innerHTML = `✅ 请求成功!

📤 发送的原始数据:
${JSON.stringify(requestData, null, 2)}

🔐 加密后的数据:
${encryptedResult.encryptedData}

🔑 加密密钥:
${encryptedResult.sm4Key}

📥 服务器响应:
${responseText}

🔍 响应头:
${JSON.stringify(responseHeaders, null, 2)}

${decryptedResponse ? `🔓 解密后的响应:
${JSON.stringify(decryptedResponse, null, 2)}` : ''}`;
                result.style.display = 'block';
                
            } catch (error) {
                console.error("请求失败:", error);
                result.className = 'result error';
                result.innerHTML = `❌ 请求失败:
${error.message}

详细错误信息:
${error.stack || '无详细信息'}`;
                result.style.display = 'block';
            } finally {
                loading.style.display = 'none';
            }
        }
    </script>
</body>
</html> 